package com.ciwong.epaper.modules.me.adapter;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ciwong.epaper.R;
import com.ciwong.epaper.common.EVariableGlobal;
import com.ciwong.epaper.common.guidedialog.BaseGuideLayerDialog;
import com.ciwong.epaper.modules.me.bean.BookDeskCataloguesBean;
import com.ciwong.epaper.modules.me.bean.MaterialBean;
import com.ciwong.epaper.modules.me.bean.SubjectBean;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.widget.MaterialItemView;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.mobilelib.application.BaseApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by keepon on 2018/8/7.
 */
public class NewBookDeskAdapter extends RecyclerView.Adapter<NewBookDeskAdapter.NewBookDeskViewHolder> {
    private Context mContext;
    private List<SubjectBean> mSubjectBeanList = new ArrayList<>();
    private List<BookDeskCataloguesBean.CataloguesBean> mCataloguesBeanList = new ArrayList<>();
    private OnDialogCreatedListener onDialogCreatedListener;
    public BaseGuideLayerDialog dialog;
    public BaseGuideLayerDialog dialog2;

    public NewBookDeskAdapter(Context context, List<SubjectBean> subjectBeanList, List<BookDeskCataloguesBean.CataloguesBean> cataloguesBeanList) {
        mContext = context;
        mSubjectBeanList.addAll(subjectBeanList);
        mCataloguesBeanList.addAll(cataloguesBeanList);
    }

    @Override
    public NewBookDeskViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        NewBookDeskViewHolder newBookDeskViewHolder = new NewBookDeskViewHolder(new MaterialItemView(mContext));
        return newBookDeskViewHolder;
    }

    @Override
    public void onBindViewHolder(NewBookDeskViewHolder holder, int position) {
        BookDeskCataloguesBean.CataloguesBean cataloguesBean = null;
        if (mCataloguesBeanList.size() > position) {
            cataloguesBean = mCataloguesBeanList.get(position);
        }
        holder.bindData(position, mSubjectBeanList.get(position), cataloguesBean);
    }

    @Override
    public int getItemCount() {
        return mSubjectBeanList != null ? mSubjectBeanList.size() : 0;
    }

    public void updateData(List<SubjectBean> subjectBeanList, List<BookDeskCataloguesBean.CataloguesBean> cataloguesBeanList) {
        mSubjectBeanList.clear();
        mSubjectBeanList.addAll(subjectBeanList);
        mCataloguesBeanList.clear();
        mCataloguesBeanList.addAll(cataloguesBeanList);
        notifyDataSetChanged();
    }

    public void updateSubjectBeanList(List<SubjectBean> subjectBeanList) {
        mSubjectBeanList.clear();
        mSubjectBeanList.addAll(subjectBeanList);
        notifyDataSetChanged();
    }

    public void updateCatalogueList(List<BookDeskCataloguesBean.CataloguesBean> cataloguesBeanList) {
        mCataloguesBeanList.clear();
        mCataloguesBeanList.addAll(cataloguesBeanList);
        notifyDataSetChanged();
    }
    private void initGuide0(final SubjectBean subjectBean, final MaterialItemView maerialItemView) {
        try {
            boolean isShowHelp = CWSys.getSharedBoolean(EConstants.SHARE_KEY_IS_SHOW_NEW_BOOK_DESK_WORDBOOK_CHANGE_NAME + BaseApplication.getInstance().getUserInfoBase().getUserId(), true);
            if (isShowHelp) {
                if (dialog == null)
                    dialog = new BaseGuideLayerDialog(mContext, R.style.DialogStyleGuide);
                View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_new_bookdesk_wordbook_changename_tips, null);
                RelativeLayout ll_dialog_inner = view.findViewById(R.id.ll_dialog_inner);
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ll_dialog_inner.getLayoutParams();
                if (layoutParams != null){
                    if (android.os.Build.MODEL.equalsIgnoreCase("MI 8")) {
                        if (layoutParams != null) {
                            layoutParams.topMargin = layoutParams.topMargin - mContext.getResources().getDimensionPixelSize(R.dimen.dp_26);
                        }
                    }
                }
                dialog.setContentView(view);
                dialog.setCancelable(false);
                dialog.findViewById(R.id.tx_perusal_know).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EVariableGlobal.CHECKED_GUIDE_DIALOG_SHOWING = false;
                        CWSys.setSharedBoolean(EConstants.SHARE_KEY_IS_SHOW_NEW_BOOK_DESK_WORDBOOK_CHANGE_NAME + BaseApplication.getInstance().getUserInfoBase().getUserId(), false);
                        dialog.dismiss();
                        dialog = null;
                        initGuide(subjectBean,maerialItemView);
                    }
                });
                if (onDialogCreatedListener != null) onDialogCreatedListener.onCreatedListener(dialog);
            }
        }catch (Exception e ){

        }

    }
    //引导层
    private void initGuide(SubjectBean subjectBean, MaterialItemView maerialItemView) {
        try {
            boolean isShowHelp = CWSys.getSharedBoolean(EConstants.SHARE_KEY_IS_SHOW_NEW_BOOK_DESK + BaseApplication.getInstance().getUserInfoBase().getUserId(), true);
            if (isShowHelp) {
                if (dialog == null)
                    dialog = new BaseGuideLayerDialog(mContext, R.style.DialogStyleGuide);
                View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_new_bookdesk_tips, null);
                LinearLayout ll_dialog_inner = view.findViewById(R.id.ll_dialog_inner);
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ll_dialog_inner.getLayoutParams();
                if (layoutParams != null){
                    if (android.os.Build.MODEL.equalsIgnoreCase("MI 8")) {
                        if (layoutParams != null) {
                            layoutParams.topMargin = layoutParams.topMargin - mContext.getResources().getDimensionPixelSize(R.dimen.dp_26);
                        }
                    }
                }
                dialog.setContentView(view);
                dialog.setCancelable(false);
                ImageView icon_subject = dialog.findViewById(R.id.icon_subject);
                TextView tv_subject_name = dialog.findViewById(R.id.tv_subject_name);
                TextView mTvVersionName = dialog.findViewById(R.id.tv_book_name);
                TextView tv_book_grade = dialog.findViewById(R.id.tv_book_grade);
                tv_subject_name.setText(subjectBean.getSubjectName());
                List<MaterialBean> materialBeanList = subjectBean.getMaterial();
                if (materialBeanList != null) {
                    for (MaterialBean materialBean : materialBeanList) {
                        if (materialBean.isSelected()) {
                            tv_book_grade.setText(maerialItemView.getGradeBookName(materialBean));
                            mTvVersionName.setText(materialBean.getVersionName());
                        }
                    }
                }
                Glide.with(mContext).load(subjectBean.getSubjectIcon()).placeholder(R.mipmap.icon_subject).into(icon_subject);
                dialog.findViewById(R.id.tx_perusal_know).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EVariableGlobal.CHECKED_GUIDE_DIALOG_SHOWING = false;
                        CWSys.setSharedBoolean(EConstants.SHARE_KEY_IS_SHOW_NEW_BOOK_DESK + BaseApplication.getInstance().getUserInfoBase().getUserId(), false);
                        dialog.dismiss();
                        dialog = null;
                    }
                });
                if (onDialogCreatedListener != null) onDialogCreatedListener.onCreatedListener(dialog);
            }
        }catch (Exception e ){

        }

    }

    public class NewBookDeskViewHolder extends RecyclerView.ViewHolder {

        public NewBookDeskViewHolder(View itemView) {
            super(itemView);
        }

        public void bindData(final int position, final SubjectBean subjectBean, final BookDeskCataloguesBean.CataloguesBean cataloguesBean) {

            if (itemView instanceof MaterialItemView) {
                MaterialItemView maerialItemView = (MaterialItemView) itemView;
                maerialItemView.bindData(subjectBean, cataloguesBean);
                maerialItemView.setOnMaterItemViewClicklistener(new MaterialItemView.OnMaterItemViewClicklistener() {
                    @Override
                    public void onClickChangeMoudle(String unitBeanUnitid) {
                        if (mOnMaterItemClicklistener != null) {
                            mOnMaterItemClicklistener.onClickChangeMoudle(position, subjectBean, cataloguesBean, unitBeanUnitid);
                        }
                    }

                    @Override
                    public void onClickChangeMaterial(MaterialBean selectedMaterialBean) {
                        if (mOnMaterItemClicklistener != null) {
                            mOnMaterItemClicklistener.onClickChangeMaterial(position, subjectBean, cataloguesBean, selectedMaterialBean);
                        }
                    }

                    @Override
                    public void onClickStartStudy() {
                        if (mOnMaterItemClicklistener != null) {
                            mOnMaterItemClicklistener.onClickStartStudy(position, subjectBean, cataloguesBean);
                        }
                    }

                    @Override
                    public void onClickRefreshCatalogue() {
                        if (mOnMaterItemClicklistener != null) {
                            mOnMaterItemClicklistener.onClickRefreshCatalogue(position, subjectBean, cataloguesBean);
                        }
                    }
                });

                if (position == 0) {
                    initGuide0(subjectBean, maerialItemView);
                }
            }
        }
    }

    public void setOnDialogCreatedListener(OnDialogCreatedListener onDialogCreatedListener) {
        this.onDialogCreatedListener = onDialogCreatedListener;
    }

    public interface OnDialogCreatedListener {
        void onCreatedListener(Dialog dialog);
    }

    public void setOnMaterItemClicklistener(OnMaterItemClicklistener onMaterItemClicklistener) {
        mOnMaterItemClicklistener = onMaterItemClicklistener;
    }

    OnMaterItemClicklistener mOnMaterItemClicklistener;

    public interface OnMaterItemClicklistener {
        void onClickChangeMoudle(int position, SubjectBean subjectBean, BookDeskCataloguesBean.CataloguesBean cataloguesBean, String currentUnitBeanUnitid);

        void onClickChangeMaterial(int position, SubjectBean subjectBean, BookDeskCataloguesBean.CataloguesBean cataloguesBean, MaterialBean materialBean);

        void onClickStartStudy(int position, SubjectBean subjectBean, BookDeskCataloguesBean.CataloguesBean cataloguesBean);

        void onClickRefreshCatalogue(int position, SubjectBean subjectBean, BookDeskCataloguesBean.CataloguesBean cataloguesBean);
    }
}
